package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 条件分页
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(SensitiveDto dto) {
        //参数检测
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //敏感词模糊查询
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (StringUtils.isNotEmpty(dto.getSensitives())) {
            wrapper.like(AdSensitive::getSensitives, dto.getSensitives());
        }
        //执行分页查询
        IPage<AdSensitive> page = page(new Page<AdSensitive>(dto.getPage(), dto.getSize()), wrapper);
        //封装数据返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());

        return responseResult;
    }

    /**
     * 新增
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult add(AdSensitive adSensitive) {
        //参数检测
        if (adSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行新增
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 修改
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数检测
        if (adSensitive == null || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断存在
        AdSensitive sensitive = getById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行修改
        updateById(adSensitive);
        return ResponseResult.okResult();
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //参数检测
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断存在
        AdSensitive sensitive = getById(id);
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //执行删除
        removeById(id);
        return ResponseResult.okResult();
    }
}